import wx from 'weixin-js-sdk';
import { fetchGet,HJchannelcode } from '../api';
export function setShare(config) {
  let webUrl = location.href,urlStr = webUrl.split('#');
  if(urlStr.length>2){webUrl = `${urlStr[0]}#${urlStr[1]}`;}
  fetchGet('WxJsSdk/GetWxSharUrl?ChannelCode=' + HJchannelcode + '&ShareUrl=' + webUrl,{}).then(resp=>{
    const jssdk = resp.Result;    
    wx.config({
      //beta: true,
      debug: false,
      appId: jssdk.AppId,
      timestamp: jssdk.Timestamp,
      nonceStr: jssdk.NonceStr,
      signature: jssdk.Signature,
      jsApiList: [
        'onMenuShareTimeline', //分享到朋友圈
        'onMenuShareAppMessage' //分享给朋友
      ]
    });
    setConfig(config);
  });
};
function setConfig(config){
  if (!config) {
    config = {
        title: '行家推荐',
        desc: '',
        link: location.href,
        imgUrl: ''
    }
  }
  wx.ready(function(){
    let shareObjs = {
      title: config.title,
      desc: config.desc,
      link: config.link,
      imgUrl: config.imgUrl,
      success: function(res){
          config.success && config.success();
      },
      cancel: function(res){
          config.cancel && config.cancel();
      },
      fail: function(res){
          config.fail && config.fail();
      }
    };
    wx.onMenuShareAppMessage(shareObjs);
    wx.onMenuShareTimeline(shareObjs);
  });
}